﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmNewKhachHang : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private bool allowCloseForm = true;
        public static int daluu = 0;
        public static KhachHangBO KhachHangBO = KhachHangBO.NewRootKhachHangBO();


        #endregion variant

        //=====================================
        #region form
        public FrmNewKhachHang()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmNewKhachHang_Load);
            this.FormClosing += new FormClosingEventHandler(FrmNewKhachHang_FormClosing);

            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.KeyDown += new KeyEventHandler(FrmNewKhachHang_KeyDown);
        }

        void FrmNewKhachHang_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10))
                this.btnClose_Click(null,null);
            if ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6))
                this.btnSave_Click(null, null);

        }

        private void FrmNewKhachHang_Load(object sender, EventArgs e)
        {
            Utility.InitControl(this.lookUpLoaiKhachHang);

            RefreshData();

        }

        private void FrmNewKhachHang_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control

        #endregion control

        //=====================================
        #region button

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            FrmNewKhachHang.daluu = 1;
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            FrmNewKhachHang.daluu = 0;
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid

        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {
            DanhMucBOList loaiKhachHangBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.NhomKhachHang);
            Utility.SetData(this.lookUpLoaiKhachHang, loaiKhachHangBOList, loaiKhachHangBOList.Count);
        }

        private void Save()
        {
            try
            {
                KhachHangBO.MaKhachHang = txtMa.Text.Trim();
                KhachHangBO.Ten = txtTen.Text.Trim();
                KhachHangBO.NhomKhachHangID = StaticUtil.GetInt(lookUpLoaiKhachHang.EditValue, -1);
                KhachHangBO.NguoiLienHe = txtNguoiLienHe.Text.Trim();
                KhachHangBO.DienThoai = txtDienThoai.Text.Trim();
                KhachHangBO.DiDong = txtDiDong.Text.Trim();
                KhachHangBO.Fax = txtFax.Text.Trim();
                KhachHangBO.Email = txtEmail.Text.Trim();
                KhachHangBO.KH = KhachHangBO.NCC = true;
                KhachHangBO.Save();
            }
            catch
            {

            }
        }
        #endregion function

    }
}